import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebFilter({"/home.jsp", "/books"})
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpSession session = req.getSession();

        String username = (String) session.getAttribute("username");

        if (username != null){
            filterChain.doFilter(servletRequest,servletResponse);

        }else {
            servletRequest.setAttribute("msg", "请登录后访问！");
            servletRequest.getRequestDispatcher("login.jsp").forward(servletRequest,servletResponse);
        }
    }
}


